
// let baseUrl = "https://fintech-wx-zxr.szjrgzjg.com";
let baseUrl = "http://192.168.1.84:20104";
// let baseUrl = "http://192.168.1.200:20104";
// let baseUrl = "http://192.168.1.46:20104"; //高坤
import des from "./des";
function request (
  url,
  data = {},
  method = "GET",
  needSecret,
  header = "application/json"
) {
  return new Promise(function (resolve, reject) {
    let dataInfos = "";
    if (!needSecret) {
      dataInfos = JSON.stringify(data);
    } else {
      dataInfos = data;
    }
    wx.request({
      url: baseUrl + url,
      data: needSecret
        ? dataInfos
        : des.encrypt(dataInfos, "26121f7667a94105b834cea22a7eb753"),
      method: method,
      header: {
        "Content-Type": header,
        token: wx.getStorageSync("token") || "cxgc123456",
      },
      success: (res) => {
        // console.log(res, "接口返回的结果");
        if (res.statusCode == 200) {
          if (needSecret) {
            resolve(res.data);
          } else {
            var data = des.decrypt(
              res.data.data,
              "26121f7667a94105b834cea22a7eb753"
            );
            // 判断账号是否唯一
            // console.log(JSON.parse(data), "JSON.parse(data)");
            if (JSON.parse(data).code == 400) {
              wx.removeStorageSync("token");
              wx.removeStorageSync("userInfo");
              wx.showToast({
                title: JSON.parse(data).message,
                icon:"none"
              })
              wx.navigateTo({
                url: "/pages/main/main?type=4",
              });
            }
            resolve(JSON.parse(data));
          }
        } else {
          reject(res.errMsg);
        }
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

module.exports = {
  request,
};
